<template>
	<view>
		<view class="con">
			<view class="title">
				<view class="label">
					标题
				</view>
				<input class="inp" type="text" v-model="form.title" placeholder="请输入标题名称">
			</view>
			<u--textarea v-model="form.content" placeholder="请输入内容详情. . ." border='none' height='255rpx'
				placeholderStyle='color: #8A8989;font-size:30rpx;'></u--textarea>
		</view>

		<view class="pic_box">
			<view class="pic_item" v-for="(item,index) in fileList6" :key="index">
				<image class="pic_bg" @click="imgPreview(fileList6,index)" :src="baseurl+item" mode="aspectFill"></image>
				<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);" @click="resup(index)">
					重新上传
				</view>
				<view class="delbox" @click="del(index)">
					<image class="del_icon" src="./static/picdel.png" mode=""></image>
				</view>
			</view>
			<view class="pic_item" @click="up">
				<image class="pic_bg" src="./static/up.png" mode=""></image>
			</view>
		</view>
		<view class="btn" @click="send">
			确认发布
		</view>
	</view>
</template>

<script>
	import {
		addSend
	} from '@/api/dymic.js'
	export default {
		data() {
			return {
				title: '',
				type: null,
				value1: '',
				fileList6: [],
				form: {
					"title": "",
					"content": "",
					"image": "",
					"type":''
				},
				baseurl: '',
			};
		},
		onLoad(option) {
			this.type=option.type
			this.baseurl = this.$BASE_URL
			uni.setNavigationBarTitle({
				title: decodeURI(option.title)
			})

		},
		methods: {
			imgPreview(urls, i) {
				console.log(urls)
				let us = []
				urls.map(t => {
					us.push(this.baseurl + t)
				})
				uni.previewImage({
					urls: us,
					current: i
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async up(type) {
				let _this = this
				await uni.chooseImage({
					count: 9,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						tempFilePaths.map(t => {
							uni.showLoading({
								title: '上传中'
							})
							uni.uploadFile({
								url: _this.$BASE_URL +
									'/api/common/upload', //仅为示例，非真实的接口地址
								filePath: t,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (uploadFileRes) => {
									this.fileList6.push(JSON.parse(uploadFileRes.data)
										.data.url)
									uni.hideLoading()
									uni.showToast({
										title: JSON.parse(uploadFileRes.data)
											.msg,
										icon: 'none'
									})
								},

							});
						})
					}
				});
			},
			del(i) {
				this.fileList6.splice(i, 1)
			},
			async resup(i) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: _this.$BASE_URL +
								'/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								uni.hideLoading()
								_this.$set(_this.fileList6, i, JSON.parse(uploadFileRes.data)
									.data
									.url)
								uni.showToast({
									title: JSON.parse(uploadFileRes.data).msg,
									icon: 'none'
								})
							},
						});
					}
				});
			},
			send() {
				// console.log(this.fileList6)
				this.form.image=this.fileList6.join(',')
				let {image,title,content} = this.form
				
				if(!title){
					uni.showToast({
						title:'请输入项目标题',
						icon:'none'
					})
					return
				}
				if(!content){
					uni.showToast({
						title:'请输入项目内容',
						icon:'none'
					})
					return
				}
				if(!image){
					uni.showToast({
						title:'请上传图片',
						icon:'none'
					})
					return
				}
				this.form.type=this.type
				addSend(this.form).then(res=>{
					if(res.data.code==1){
						uni.showToast({
							title:'发布成功',
							icon:'none'
						})
						this.form.title=''
						this.form.content=''
						this.form.image=''
						this.fileList6=[]
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 3]; //上上一页页面实例
						prevPage.$vm.page = 1
						prevPage.$vm.list = []
						prevPage.$vm.getlist()
						uni.navigateBack({
							delta:2
						})
					}
				})
				// console.log(this.image)
			}
		}
	}
</script>

<style lang="less">
	.con {
		padding: 0 34rpx 0 22rpx;
	}

	.title {
		border-bottom: 3rpx solid #F6F6F6;
		line-height: 132rpx;
		display: flex;
		align-items: center;

		.label {
			color: #030303;
			font-size: 36rpx;
		}

		.inp {
			font-size: 32rpx;
			flex: 1;
			text-align: right;
		}
	}

	.upload {
		padding: 0 38rpx;
	}

	.btn {
		width: 687rpx;
		line-height: 114rpx;
		background: linear-gradient(153deg, #FF8717, #FF5F00);
		border-radius: 57rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 36rpx;
		position: fixed;
		bottom: 118rpx;
		left: 31rpx;
	}

	.pic_box {
		display: flex;
		flex-wrap: wrap;
		padding: 0 38rpx;
		.pic_item {
			width: 198rpx;
			height: 198rpx;
			position: relative;
			margin-right: 30rpx;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			overflow: hidden;
		}

		.pic_bg {
			width: 198rpx;
			height: 198rpx;
			position: absolute;
			border-radius: 10rpx;
			top: 0;
			left: 0;
		}

		.pic_item_name {
			text-align: center;
			width: 100%;
			position: absolute;
			bottom: 0;
			color: #fff;
			font-size: 26rpx;
			line-height: 60rpx;
		}
	}

	.delbox {
		width: 29rpx;
		height: 25rpx;
		background: rgba(0, 0, 0, .5);
		border-radius: 7rpx 0 0 12rpx;

		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 0;
		right: 0;
	}

	.del_icon {
		width: 12rpx;
		height: 12rpx;
	}
</style>
